<template>
    <div class="login-container">
        <div class="login-box">
            <h2>用户登录</h2>
            <el-form :model="loginForm" :rules="rules" ref="loginForm">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" placeholder="用户名">
                        <template #prefix>
                            <UserOutlined class="site-form-item-icon" />
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" type="password" placeholder="密码">
                        <template #prefix>
                            <LockOutlined class="site-form-item-icon" />
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleLogin" :loading="loading" block>登录</el-button>
                </el-form-item>
                <div class="register-link">
                    <router-link to="/register">还没有账号？立即注册</router-link>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    name: 'LoginPage',
    data() {
        return {
            loginForm: {
                username: '',
                password: ''
            },
            loading: false,
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        handleLogin() {
            this.$refs.loginForm.validate(async valid => {
                if (valid) {
                    this.loading = true
                    try {
                        await this.$store.dispatch('user/login', this.loginForm)
                        this.$message.success('登录成功')
                        
                        // 获取重定向地址或默认跳转到首页
                        const redirect = this.$route.query.redirect || '/'
                        this.$router.push(redirect)
                    } catch (error) {
                        this.$message.error(error.message || '登录失败')
                        console.error('登录错误:', error)
                    } finally {
                        this.loading = false
                    }
                }
            })
        }
    }
}
</script>

<style scoped>
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f5f7fa;
}

.login-box {
    width: 400px;
    padding: 30px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}

.login-box h2 {
    text-align: center;
    margin-bottom: 30px;
    color: #303133;
}

.register-link {
    text-align: center;
    margin-top: 15px;
}
</style> 